/*
 * @Author: 橘子
 * @Date: 2022-09-30 21:54:25
 * @Project_description: 项目介绍
 * @Description: 代码是我抄的，不会也是真的
 */
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import util from '@/libs/util'
import setting from '@/setting'

if (setting.menu.locales) {
  Vue.use(VueI18n)
}

function loadLocaleMessages() {
  const locales = require.context('./locales', true, /[A-Za-z0-9-_,\s]+\.json$/i)
  const messages = {}
  for (const key of locales.keys()) {
    const matched = key.match(/([A-Za-z0-9-_]+)\./i)
    if (matched && matched.length > 1) {
      const locale = matched[1]
      const localeElementUI = require(`element-ui/lib/locale/lang/${locales(key)._element}`)
      messages[locale] = {
        ...locales(key),
        ...(localeElementUI ? localeElementUI.default : {}),
      }
    }
  }
  return messages
}

const messages = loadLocaleMessages()

Vue.prototype.$languages = Object.keys(messages).map((langlage) => ({
  label: messages[langlage]._name,
  value: langlage,
}))
//  启用i18n
let i18n = []
if (setting.menu.locales) {
  i18n = new VueI18n({
    locale: util.cookies.get('lang') || setting.APP.i18n,
    fallbackLocale: setting.APP.i18nLocale,
    messages,
  })
}

export default i18n
